package com.example.controller;

import com.example.common.Result;
import com.example.entity.AirQualitySensor;
import com.example.entity.InfraredSensor;
import com.example.entity.LightSensor;
import com.example.entity.Params;
import com.example.service.InfraredSensorService;
import com.example.service.LightSensorService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@ResponseBody
@RestController
@CrossOrigin
@RequestMapping("/LS")
public class LightSensorController {

    @Resource
    private LightSensorService lightSensorService;


    @GetMapping("/getSensorData")
    public Result getSensorData(Params params) {
        PageInfo<LightSensor> search = lightSensorService.getSensorData(params);
        return Result.success(search);
    }

    @PostMapping("/addLS")
    public Result addLS(@RequestBody LightSensor lightSensor){
       lightSensorService.addLS(lightSensor);
        return Result.success();
    }

    @DeleteMapping("/deleteLS/{id}")
    public Result deleteLS(@PathVariable Long id){
        lightSensorService.deleteLS(id);
        return Result.success();
    }

    @GetMapping("/echarts/line")
    public Result line(Params params) {
        // 防止 interval 传了非法数值，比如负数0小时
        if (params.getInterval() == null || params.getInterval() <= 0 || params.getInterval() > 24) {
            params.setInterval(3) ; // 给一个合理默认值
        }
        Map<String, Object> data = lightSensorService.initLine(params);
        return Result.success(data);
    }

    @GetMapping("/echarts/initLineForAdmin")
    public Result initLineForAdmin(Params params) {
        if (params.getPast() == null || params.getPast() <= 0) {
            params.setPast(3); ; // 给一个合理默认值
        }
        Map<String, Object> data = lightSensorService.initLineForAdmin(params);
        return Result.success(data);
    }
}
